package com.lbs.tdrive;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lbs.tdrive.database.DbManager;


/**
 * Servlet implementation class Distance
 */
@WebServlet("/Distance")
public class Distance extends HttpServlet {
	private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Distance() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String latStr1=request.getParameter("lat1");
		String longStr1=request.getParameter("long1");
		String latStr2=request.getParameter("lat2");
		String longStr2=request.getParameter("long2");
		String startTimeStr = request.getParameter("starttime");
		String endTimeStr = request.getParameter("endtime");
		String distanceStr = request.getParameter("distance");
		double lat1=0;
		double long1=0;
		if(latStr1!=null&&longStr1!=null){
			lat1=Double.parseDouble(latStr1);
			long1=Double.parseDouble(longStr1);
			
			
		}
		double lat2=0;
		double long2=0;
		if(latStr2!=null&&longStr2!=null){
			lat2=Double.parseDouble(latStr2);
			long2=Double.parseDouble(longStr2);
			
			
		}
		
	   long startTime=0;
	   long endTime=0;
	   long totalTime = -1;
	   
	   if(startTimeStr!=null && endTimeStr!=null){
		   startTime = Long.parseLong(startTimeStr);
		   endTime = Long.parseLong(endTimeStr);
		   totalTime = endTime -startTime;
	   }
		
	

	   double distance = distance(lat1, long1, lat2, long2, 'K')*1000;
	   if(distanceStr!=null){
		   distance = Double.parseDouble(distanceStr);
	   }
	   if( distance >=MINIMUM_DISTANCE_CHANGE_FOR_UPDATES) {
	   if(latStr1!=null&&longStr1!=null&&
			   latStr2!=null&&longStr2!=null&&
			   startTimeStr!=null && endTimeStr!=null) {
		  DbManager db = DbManager.getInstance();
		  db.saveData(lat1, long1, lat2, long2, startTime, endTime, distance, totalTime);
		  System.out.println("Distance = "+distance);
		   
	   }
	   } else {
		   System.out.println("Distance not met = "+distance);
	   }
	  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	
	private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
	      double theta = lon1 - lon2;
	      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + 
	    		  Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
	    		  Math.cos(deg2rad(theta));
	      dist = Math.acos(dist);
	      dist = rad2deg(dist);
	      dist = dist * 60 * 1.1515;
	      if (unit == 'K') {
	        dist = dist * 1.609344;
	      } else if (unit == 'N') {
	        dist = dist * 0.8684;
	        }
	      return (dist);
	    }

	    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	    /*::  This function converts decimal degrees to radians             :*/
	    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	    private double deg2rad(double deg) {
	      return (deg * Math.PI / 180.0);
	    }

	    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	    /*::  This function converts radians to decimal degrees             :*/
	    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
	    private double rad2deg(double rad) {
	      return (rad * 180.0 / Math.PI);
	    }

	    
}
